package com.edu.servlet;

import com.edu.dao.CollectionDao;
import com.edu.dao.impl.collectionDaoImpl;
import com.edu.entity.goodsCollection;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/CollectionServlet")
public class CollectionServlet extends HttpServlet {
    private CollectionDao collectionDao = new collectionDaoImpl();
    private Gson gson = new Gson();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        
        String action = request.getParameter("action");
        PrintWriter out = response.getWriter();
        
        try {
            if ("getCollections".equals(action)) {
                // 获取分页收藏数据
                int userId = Integer.parseInt(request.getParameter("userId"));
                
                List<goodsCollection> collections = collectionDao.getGoods(userId);
                int total = collectionDao.getCollectionAll(userId);
                
                Map<String, Object> result = new HashMap<>();
                result.put("code", 200);
                result.put("msg", "success");
                result.put("data", collections);
                result.put("total", total);
                out.print(gson.toJson(result));
            } // 在 doPost 方法中添加 deleteCollection 分支
            else if ("deleteCollection".equals(action)) {
                int userId = Integer.parseInt(request.getParameter("userId"));
                int goodsId = Integer.parseInt(request.getParameter("goodsId"));
                System.out.println(userId);
                System.out.println(goodsId);
                boolean success = collectionDao.deleteCollection(userId, goodsId);
                
                Map<String, Object> result = new HashMap<>();
                if (success) {
                    result.put("code", 200);
                    result.put("msg", "取消收藏成功");
                } else {
                    result.put("code", 500);
                    result.put("msg", "取消收藏失败");
                }
                out.print(gson.toJson(result));
            }
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("code", 500);
            result.put("msg", "服务器错误: " + e.getMessage());
            out.print(gson.toJson(result));
        } finally {
            out.flush();
            out.close();
        }
    }
}